<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="项目名称" prop="activity_name">
        <el-input v-model="form.activity_name" />
      </el-form-item>
      <el-form-item
        label="公司名称"
        prop="company_name"
      >
        <el-input v-model="form.company_name" />
      </el-form-item>
      <el-form-item label="项目经理" prop="project_manager">
        <el-input v-model="form.project_manager" />
      </el-form-item>
      <el-form-item label="起止日期" prop="activity_range">
        <el-date-picker
          v-model="form.activity_range"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doAdd, doEdit } from '@/api/activity'
  import { getStaticUserInfo } from '@/utils/user'

  const staticUserInfo = getStaticUserInfo()

  export default {
    name: 'ActivityEdit',
    data() {
      return {
        form: {
          activity_name: '',
          // 因为每个公司登录的都是自己的公司账户，所以这里没有必要出现公司名称
          company_name: staticUserInfo.company_name || '',
          project_manager: '',
          activity_start: '',
          activity_end: '',
          activity_range: null,
        },
        rules: {
          activity_name: [
            { required: true, trigger: 'blur', message: '请输入项目名称' },
          ],
          company_name: [
            { required: true, trigger: 'blur', message: '请输入公司名称' },
          ],
          project_manager: [
            { required: true, trigger: 'blur', message: '请输入项目经理' },
          ],
          activity_range: [
            { required: true, trigger: 'change', message: '请选择起止日期' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        type: null,
      }
    },
    watch: {
      'form.activity_range'(range) {
        if (range) {
          this.form.activity_start = range[0]
          this.form.activity_end = range[1]
        } else {
          this.form.activity_start = ''
          this.form.activity_end = ''
        }
      },
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
          this.type = 'add'
        } else {
          this.title = '编辑'
          this.type = 'edit'
          const activity_range = [row.activity_start, row.activity_end]
          this.form = Object.assign({ activity_range }, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            let msg
            if (this.type === 'add') msg = await doAdd(this.form)
            else msg = await doEdit({ ...this.form, activity_id: this.form.id })
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
